package cn.laike.modules.dev.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.Query;
import cn.laike.modules.dev.dao.EarnestMoneyOrderDao;
import cn.laike.modules.dev.entity.EarnestMoneyOrderEntity;
import cn.laike.modules.dev.service.EarnestMoneyOrderService;
import cn.laike.modules.sys.shiro.ShiroUtils;


@Service("earnestMoneyOrderService")
public class EarnestMoneyOrderServiceImpl extends ServiceImpl<EarnestMoneyOrderDao, EarnestMoneyOrderEntity> implements EarnestMoneyOrderService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	
    	Integer hotelId = ShiroUtils.getHotelId();
    	String name = (String)params.get("name");
    	String mobile = (String)params.get("mobile");
		String startTime = (String)params.get("startTime");
    	String endTime = (String)params.get("endTime");
        Page<EarnestMoneyOrderEntity> page = this.selectPage(
                new Query<EarnestMoneyOrderEntity>(params).getPage(),
                new EntityWrapper<EarnestMoneyOrderEntity>()
                .eq(ShiroUtils.isAdmin(),"uid", ShiroUtils.getUserId())
                .eq((hotelId != null && hotelId > 0), "hotel_id", hotelId)
                .like(StringUtils.isNotBlank(name),"name", name)
                .like(StringUtils.isNotBlank(mobile),"mobile", mobile)
                .between((StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)),"create_time", startTime + " 00:00:00", endTime + " 23:59:59")
                .ne("status", 0)
                .orderBy("id", false)
        );

        return new PageUtils(page);
    }

	@Override
	public EarnestMoneyOrderEntity getByOrderNo(String orderNo) {
		List<EarnestMoneyOrderEntity> list = this.selectList(new EntityWrapper<EarnestMoneyOrderEntity>()
    			.eq("order_no", orderNo));
    	return (list != null && list.size() > 0) ? list.get(0) : null;
	}

	@Override
	public Double getMyTotalMoney(String openid) {
		return this.baseMapper.getMyTotalMoney(openid);
	}
	
	@Override
	public List<Double> statTotalMoney(Long uid, Integer hotelId, String startTime, String endTime) {
		return this.baseMapper.statTotalMoney(uid, hotelId, startTime, endTime);
	}

}
